package com.oc.mba.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.oc.mba.util.CommConsts;
import com.oc.mba.util.JsonUtil;

public class BaseController extends MultiActionController {

	/**
	 * 生成Error的JSON格式字符
	 */
	protected String genError(int errorCode) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put(CommConsts.RESULT_FLAG, errorCode);
		return JsonUtil.jsonMTS(result);
	}
	
	/**
	 * 返回JSON格式数据
	 */
	protected void resultJson(HttpServletResponse response, String result) throws IOException {
		response.getWriter().print(result);
	}
	
	/**
	 * 返回xml
	 */
	protected void resultXml(HttpServletResponse response, String result) throws IOException {
		response.getWriter().print(result);
	}
	
	/**
	 * 校验是否是合法请求
	 * 
	 * @param reqJSON 请求的参数
	 * @return true:合法,false:不合法
	 */
	protected boolean isValidReq(JSONObject reqJSON) {
		if (reqJSON == null) {
			return false;
		}
		// 是否包含分发标签
		if (!reqJSON.containsKey(CommConsts.SIGNID_FLAG)) {
			return false;
		}
		return true;
	}
	
	/**
	 * 封装的请求
	 * @author unidw
	 */
	class WrappedReq {
		
		private String signID;
		private JSONObject content;

		public String getSignID() {
			return signID;
		}

		public void setSignID(String signID) {
			this.signID = signID;
		}

		public JSONObject getContent() {
			return content;
		}

		public void setContent(JSONObject content) {
			this.content = content;
		}

		@Override
		public String toString() {
			return "WrappedReq [signID=" + signID + ", content=" + content
					+ "]";
		}
		
	}
	
}
